package com.liuyu.common.framework.utils;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * 公共utils
 *
 * @author: liuyu
 * @date: 2024/9/23 16:02
 * @version: 1
 */
public class CommonUtil {
    public static final String GENERAL = "General";
    public static final int CELL_DATAFORMAT_DATE = 58;
    public static final String MSIE = "MSIE";
    public static final String MOZILLA = "Mozilla";
    public static final String FIREFOX = "firefox";

    private CommonUtil() {
    }

    public static <T> void removeDuplicate(List<T> list) {
        LinkedHashSet<T> set = new LinkedHashSet(list.size());
        set.addAll(list);
        list.clear();
        list.addAll(set);
    }



    public static String setCharAtPos(String str, int pos, char c) {
        if (str == null) {
            str = "";
        }

        if (pos > 100) {
            throw new IllegalArgumentException("非法参数,pos不能大于100");
        } else if (pos < 0) {
            return str;
        } else if (pos < str.length()) {
            return str.substring(0, pos) + c + str.substring(pos + 1);
        } else if (pos == str.length()) {
            return str.substring(0, pos) + c;
        } else {
            StringBuffer buf = new StringBuffer(str);

            for (int i = str.length(); i < pos; ++i) {
                buf.append(' ');
            }

            buf.append(c);
            return buf.toString();
        }
    }

    public static <T> void clearNull(List<T> list) {
        Collection nuCon = new Vector();
        nuCon.add((Object) null);
        list.removeAll(nuCon);
    }

    public static <T> List<? extends T> removeNull(List<? extends T> oldList) {
        oldList.removeAll(Collections.singleton((Object) null));
        return oldList;
    }

    public static boolean isAjax(final HttpServletRequest request) {
        boolean isAjaxRequest = false;
        if (!StringUtils.isBlank(request.getHeader("x-requested-with")) && request.getHeader("x-requested-with").equals("XMLHttpRequest")) {
            isAjaxRequest = true;
        }

        return isAjaxRequest;
    }

    public static String getFileSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    public static String getFileSuffix(File file) {
        String fileName = file.getName();
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    public static void compatibleFileName(HttpServletRequest request, HttpServletResponse response, String excelname) throws UnsupportedEncodingException {
        String agent = request.getHeader("USER-AGENT").toLowerCase();
        if (agent.contains("firefox")) {
            response.setCharacterEncoding("utf-8");
            response.setHeader("content-disposition", "attachment;filename=" + new String(excelname.getBytes(), "ISO8859-1"));
        } else {
            String codedFileName = URLEncoder.encode(excelname, "UTF-8");
            response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
        }

        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    }
}
